import { Controller } from 'egg';

const request = require('request');

export default class MvController extends Controller {

    public async index() {
        const { ctx } = this;
        ctx.body = await ctx.service.test.sayHi('egg');
    }

    // 最新MV
    public async first() {
        const data = {
            total:true,
            limit:this.ctx.query.limit || 30
        }
        const url = `/weapi/mv/first`;
        let result = await this.service.mequest.createWebAPIRequest(url, 'POST', data);
        this.ctx.body = result.body;
    }

    //MV排行
    public async top(){
        const data = {
            offset: this.ctx.query.offset || 0,
            total: true,
            limit: this.ctx.query.limit || 30
        }
        const url =  `/weapi/mv/toplist`   ;
        let result = await this.service.mequest.createWebAPIRequest(url, 'POST', data);
        this.ctx.body = result.body;
    }
    
    // 获取MV数据
    public async detail(){
        const data = {id:this.ctx.query.id};
        const url = `/weapi/mv/detail`;
        let result = await this.service.mequest.createWebAPIRequest(url, 'POST', data);
        this.ctx.body = result.body;
    }
    // 获取视频数据
    public async video(){
        const {id}=this.ctx.query;
        const url = `/api/cloudvideo/playurl?ids=['${id}']&resolution=720`;
        let result = await this.service.mequest.createWebAPIRequest(url, 'POST', {});
        this.ctx.body = result.body;
    }
    // 播放MV/视频
    public async play(){
        const url = this.ctx.query.url;
        const headers = {
            Referer: "http://music.163.com/",
            Cookie: "appver=1.5.0.75771;",
            "Content-Type": "video/mp4",
            Location: url
          };
          const options = {
            header: headers,
            url: url
          };
          request(options)
          .on("error", err => {
                this.ctx.body=err
          })
        //   .pipe(res);

    }

}
